package cn.genmer.test.security.algorithm.cas;


import sun.misc.Unsafe;

import java.lang.reflect.Field;

/**
 * 解决超卖问题的cas方法
 */
public class UnsafeInstance {
    //创建Unsafe魔法类：绕过虚拟机，直接操作底层
    public static Unsafe getInstance(){
        try {
            Field field = Unsafe.class.getDeclaredField("theUnsafe");
            //允许私有字段通过反射获取
            field.setAccessible(true);
            return (Unsafe) field.get(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
